<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 会员操作功能
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

quanli_require('module/base/customer/custMessage.class.php');
quanli_require('module/base/customer/quanliCust.class.php');

class pa_message{

    public function execute(){

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            $operFlg = $_REQUEST['operFlg'];

            switch ( $operFlg ) {

                case 1: //会员列表
                    $this->messageList();
                    break;

                case 2: //导入会员消息
                    $this->importMessage();
                    break;

                case 3: //详细会员消息信息
                    $this->showCustomer();
                    break;

                default:
                    backendError();
                    break;
            }

        } else {
            backendError();
        }

    }

    /**
     * 页面列表
     */
    private function messageList() {

        global $G_PGLOB;

        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(3009));

        $G_PGLOB->smarty->display('customer/messageList.tpl');

    }

    private function showCustomer() {

        global $G_PGLOB;

        if(isset($_REQUEST['message_id']) && !quanli_is_int($_REQUEST['message_id'])){
            backendError();
        }

        $G_PGLOB->smarty->assign('message_id',$_REQUEST['message_id']);

        //实例会员消息类
        $custMessage = new custMessage();

        $item_array = $custMessage->searchCustMessageMaster( $_REQUEST );

        $message_title = $item_array[0]['message_title'];

        $G_PGLOB->smarty->assign('message_title', $message_title);

        $G_PGLOB->smarty->display('customer/addMessageMaster.tpl');

    }

    /**
     * 导入会员消息
     */
    private function importMessage() {

        if(!isset($_REQUEST['message_id']) || !is_numeric($_REQUEST['message_id'])) {
            die('<script>parent.message.importMsg("参数错误", 0);</script>');
        }

        quanli_include('module/base/common/quanliFile.class.php');

        //实例文件
        $quanliFile = new quanliFile;

        /**
         * 得到上传文件，并保存为新文件在服务器上
         */
        $upload_result_array = $quanliFile->get_upload_file( 'coupon' );
        if ($upload_result_array['code'] != 200) {
            die('<script>parent.message.importMsg("' . $upload_result_array['message'] . '", 0);</script>');
        }


        /**
         * 读取Excel文件内容
         */
        $data_array = $quanliFile->readerExcel( $upload_result_array['data'] );
        if ($data_array['code'] != 200) {
            die('<script>parent.message.importMsg("' . $data_array['message'] . '", 0);</script>');
        }
        if (count($data_array['data']) > 2000) {
            die('<script>parent.message.importMsg("每次最多可导入2000条数据", 0);</script>');
        }


        /**
         * ======================================
         * 发放会员消息
         * ======================================
         */
        //实例化会员消息类
        $custMessage = new custMessage();

        $result_array = $custMessage->createCustMessage($_REQUEST['message_id'], $data_array['data']);

        if ($result_array['code'] != 200) {
            die("<script>parent.message.importMsg('" . $result_array['message'] . "', 0);</script>");
        } else {
            die("<script>parent.message.importMsg('导入成功',0);</script>");
        }

    }

}